package cn.fintecher.pangolin.service.business.model;

import cn.fintecher.pangolin.common.enums.CaseType;
import cn.fintecher.pangolin.common.enums.PayStatus;
import cn.fintecher.pangolin.common.utils.ZWDateUtil;
import cn.fintecher.pangolin.entity.mysql.QCaseInfo;
import com.querydsl.core.BooleanBuilder;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;
import java.util.Objects;

/**
 * @Author wangxiangdong
 * @Description JPA公共查询条件封装
 * @Date 2019/9/2 15:46
 **/
@Data
public class BaseQueryRequest {

    @ApiModelProperty("委托方id")
    private String principalId;

    @ApiModelProperty("批次号")
    private String batchNumber;

    @ApiModelProperty("客户姓名")
    private String personalName;

    @ApiModelProperty("身份证号码")
    private String certificateNo;

    @ApiModelProperty("卡号")
    private String cardNo;

    @ApiModelProperty("案件编号")
    private String caseNumber;

    @ApiModelProperty("手机号")
    private String phone;

    @ApiModelProperty(value = "委案阶段")
    private String stage;

    @ApiModelProperty(notes = "退案天数")
    private Integer retireCaseDays;

    @ApiModelProperty("单位名称")
    private String employerName;

    @ApiModelProperty("地址")
    private String address;

    @ApiModelProperty(notes = "委案金额")
    private Double overdueAmtTotalStart;

    @ApiModelProperty(notes = "委案金额")
    private Double overdueAmtTotalEnd;

    @ApiModelProperty(notes = "欠款金额开始")
    private Double leftAmtStart;

    @ApiModelProperty(notes = "欠款金额结束")
    private Double leftAmtEnd;

    @ApiModelProperty("委案日期开始")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date delegationDateStart;

    @ApiModelProperty("委案日期结束")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date delegationDateEnd;

    @ApiModelProperty("逾期期数开始")
    private Integer overduePeriodsStart;

    @ApiModelProperty("逾期期数结束")
    private Integer overduePeriodsEnd;

    @ApiModelProperty("逾期天数开始")
    private Integer overdueDaysStart;

    @ApiModelProperty("逾期天数结束")
    private Integer overdueDaysEnd;

    @ApiModelProperty("城市")
    private String city;

    @ApiModelProperty("退案日期开始")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date endCaseDateStart;

    @ApiModelProperty("退案日期结束")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date endCaseDateEnd;

    @ApiModelProperty("跟进日期开始")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date followTimeStart;

    @ApiModelProperty("跟进日期结束")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date followTimeEnd;

    private PayStatus payStatus;

    @ApiModelProperty(notes = "案件类型")
    private CaseType caseType;

    public BooleanBuilder generateBaseQueryBuilder() {

        DateTime dateTime = new DateTime(ZWDateUtil.getNowDate());
        BooleanBuilder booleanBuilder = new BooleanBuilder();
        QCaseInfo caseInfo = QCaseInfo.caseInfo;
        if (StringUtils.isNoneBlank(this.principalId)) {
            booleanBuilder.and(caseInfo.principalId.eq(this.principalId));
        }
        if (StringUtils.isNoneBlank(this.batchNumber)) {
            booleanBuilder.and(caseInfo.batchNumber.like(StringUtils.join("%", this.batchNumber, "%")));
        }
        if (Objects.nonNull(this.payStatus)) {
            booleanBuilder.and(caseInfo.payStatus.eq(payStatus));
        }
        if (StringUtils.isNoneBlank(this.personalName)) {
            booleanBuilder.and(caseInfo.personalName.like(StringUtils.join("%", this.personalName, "%")));
        }
        if (StringUtils.isNoneBlank(this.employerName)) {
            booleanBuilder.and(caseInfo.employerName.like(StringUtils.join("%", this.employerName, "%")));
        }
        if (Objects.nonNull(this.caseType)) {
            booleanBuilder.and(caseInfo.caseType.eq(this.caseType));
        }
        if (StringUtils.isNoneBlank(this.certificateNo)) {
            booleanBuilder.and(caseInfo.certificateNo.like(StringUtils.join("%", this.certificateNo, "%")));
        }
        if (StringUtils.isNoneBlank(this.cardNo)) {
            booleanBuilder.and(caseInfo.cardNo.like(StringUtils.join("%", this.cardNo, "%")));
        }
        if (Objects.nonNull(this.retireCaseDays)) {
            booleanBuilder.and(caseInfo.endCaseDate.eq(dateTime.plusDays(retireCaseDays).toDate()));
        }
        if (StringUtils.isNoneBlank(this.phone)) {
            booleanBuilder.and(caseInfo.phone.like(StringUtils.join("%", this.phone, "%")));
        }
        if (StringUtils.isNoneBlank(this.caseNumber)) {
            booleanBuilder.and(caseInfo.caseNumber.eq( this.caseNumber));
        }
        if (StringUtils.isNoneBlank(this.address)) {
            booleanBuilder.and(caseInfo.address.like(StringUtils.join("%", this.address, "%")));
        }
        if (StringUtils.isNoneBlank(this.city)) {
            booleanBuilder.and(caseInfo.city.like(StringUtils.join("%", this.city, "%")));
        }
        if (Objects.nonNull(this.overduePeriodsStart)) {
            booleanBuilder.and(caseInfo.overduePeriods.goe(this.overduePeriodsStart));
        }
        if (Objects.nonNull(this.overduePeriodsEnd)) {
            booleanBuilder.and(caseInfo.overduePeriods.loe(this.overduePeriodsEnd));
        }
        if (Objects.nonNull(this.overdueDaysStart)) {
            booleanBuilder.and(caseInfo.overdueDays.goe(this.overdueDaysStart));
        }
        if (Objects.nonNull(this.overdueDaysEnd)) {
            booleanBuilder.and(caseInfo.overdueDays.loe(this.overdueDaysEnd));
        }
        if (Objects.nonNull(this.followTimeStart)) {
            booleanBuilder.and(caseInfo.followTime.goe(this.followTimeStart));
        }
        if (Objects.nonNull(this.followTimeEnd)) {
            booleanBuilder.and(caseInfo.followTime.loe(new Date(this.followTimeEnd.getTime() + 86400000)));
        }
        if (Objects.nonNull(this.overdueAmtTotalStart)) {
            booleanBuilder.and(caseInfo.overdueAmtTotal.goe(this.overdueAmtTotalStart));
        }
        if (Objects.nonNull(this.overdueAmtTotalEnd)) {
            booleanBuilder.and(caseInfo.overdueAmtTotal.loe(this.overdueAmtTotalEnd));
        }
        if (Objects.nonNull(this.delegationDateStart)) {
            booleanBuilder.and(caseInfo.delegationDate.goe(this.delegationDateStart));
        }
        if (Objects.nonNull(this.delegationDateEnd)) {
            booleanBuilder.and(caseInfo.delegationDate.loe(this.delegationDateEnd));
        }
        if (Objects.nonNull(this.endCaseDateStart)) {
            booleanBuilder.and(caseInfo.endCaseDate.goe(this.endCaseDateStart));
        }
        if (Objects.nonNull(this.endCaseDateEnd)) {
            booleanBuilder.and(caseInfo.endCaseDate.loe(this.endCaseDateEnd));
        }
        if (StringUtils.isNoneBlank(this.stage)) {
            booleanBuilder.and(caseInfo.stage.like(StringUtils.join("%", this.stage, "%")));
        }
        if (Objects.nonNull(this.leftAmtStart)) {
            booleanBuilder.and(caseInfo.leftAmt.goe(this.leftAmtStart));
        }
        if (Objects.nonNull(this.leftAmtEnd)) {
            booleanBuilder.and(caseInfo.leftAmt.loe(this.leftAmtEnd));
        }
        return booleanBuilder;
    }

}
